package com.hbvtc.xxxy.nikeSale.frames;

import java.awt.Font;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.hbvtc.xxxy.nikeSale.utils.DbManager;

public class UpdateSalesmanNike extends JFrame implements ActionListener {
	JLabel lalId, lalPwd, lalSalary, lalPost;
	JTextField jt1, jt2, jt3;
	List list1;
	JButton jButton;
	Connection conn;
	PreparedStatement pst;
	ResultSet rs;
	String sql1,sql2;

	public UpdateSalesmanNike() {
		this.setTitle("修改员工信息");
		this.setSize(1000, 700);
		this.setVisible(true);
		this.setLocationRelativeTo(null);
		// 设置标题栏的图标
		ImageIcon imageIcon = new ImageIcon(getClass().getResource("01.png"));
		this.setIconImage(imageIcon.getImage());
		// 背景图片的路径
		String path = "images/001.jpg";
		ImageIcon background = new ImageIcon(path);
		// 把背景图片显示在一个标签里面
		JLabel label = new JLabel(background);
		// 把标签的大小位置设置为图片刚好填充整个面板
		label.setBounds(0, 0, this.getWidth(), this.getHeight());
		// 把内容窗格转化为JPanel，否则不能用方法setOpaque()来使内容窗格透明
		JPanel imagePanel = (JPanel) this.getContentPane();
		imagePanel.setOpaque(false);
		// 把背景图片添加到分层窗格的最底层作为背景
		this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));

		lalId = new JLabel("请选择员工编号：");
		lalId.setFont(new Font(null, 0, 24));
		lalPwd = new JLabel("新的员工密码：");
		lalPwd.setFont(new Font(null, 0, 24));
		lalSalary = new JLabel("新的员工工资：");
		lalSalary.setFont(new Font(null, 0, 24));
		lalPost = new JLabel("新的员工职位：");
		lalPost.setFont(new Font(null, 0, 24));
		jt1 = new JTextField();
		jt2 = new JTextField();
		jt3 = new JTextField();
		jButton = new JButton("立 即 修 改");
		list1 = new List();
		list1.setFont(new Font(null, 0, 24));
		conn=DbManager.getconn();
		sql1="select SalesmanId from salesman";
		try {
			pst=conn.prepareStatement(sql1);
			rs=pst.executeQuery();
			while(rs.next()){
				list1.add(String.valueOf(rs.getInt("SalesmanId")));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		this.add(lalId);
		this.add(lalPwd);
		this.add(lalSalary);
		this.add(lalPost);
		this.add(jt1);
		this.add(jt2);
		this.add(jt3);
		this.add(jButton);
		this.add(list1);
		// ========登录的布局==============
		this.setLayout(null);

		list1.setBounds(400, 120, 250, 30);
		lalId.setBounds(140, 120, 200, 30);
		lalId.setFont(new Font(null, 0, 24));
		lalPwd.setBounds(140, 240, 200, 30);
		lalPwd.setFont(new Font(null, 0, 24));
		lalSalary.setBounds(140, 360, 200, 30);
		lalSalary.setFont(new Font(null, 0, 24));
		lalPost.setBounds(140, 480, 200, 30);
		lalPost.setFont(new Font(null, 0, 24));
		jt1.setBounds(400, 240, 250, 30);
		jt1.setFont(new Font(null, 0, 24));
		jt2.setBounds(400, 360, 250, 30);
		jt2.setFont(new Font(null, 0, 24));
		jt3.setBounds(400, 480, 250, 30);
		jt3.setFont(new Font(null, 0, 24));
		jButton.setBounds(670, 550, 180, 50);
		jButton.setFont(new Font(null, 0, 24));

		jButton.addActionListener(this);
	}

	public static void main(String[] args) {
		new UpdateSalesmanNike().setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		int size = Integer.parseInt(list1.getSelectedItem());
		String j1=jt1.getText();
		double j2 = Integer.parseInt(jt2.getText());
		String j3 = jt3.getText();
		 sql2 = "update salesman set Pwd=?,BaseSalary=?,Role=? where SalesmanId=?;";
		try {
			pst = conn.prepareStatement(sql2);
			pst.setString(1, j1);
			pst.setDouble(2, j2);
			pst.setString(3, j3);
			pst.setInt(4, size);
			pst.execute();
			JOptionPane.showMessageDialog(UpdateSalesmanNike.this, "修改成功");
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

}
